package com.cn.qgx428.lazydiary.network

import com.cn.qgx428.lazydiary.network.service.BillService
import com.cn.qgx428.lazydiary.network.service.DiaryService
import com.cn.qgx428.lazydiary.network.service.PlanService
import com.cn.qgx428.lazydiary.network.service.UserService
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import rx.internal.schedulers.NewThreadWorker

/**
 * Created by qinguanxiong on 2018/1/10.
 */

/**
 * 管理网络请求接口
 */
class NetworkManager private constructor(){

    private val client = OkHttpClient()
            .newBuilder()
            // -> 需要就加拦截器
            .build()

    private val retrofit = Retrofit.Builder()
            .baseUrl(address)
            .client(client)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())
            .build()

    companion object {
        fun getInstance() = Singleton.instant
        // 模拟器访问地址
        public val address = "http://10.0.2.2/lazyDiary/App/"
        // 真机访问地址,  手机和电脑链接同一wifi， 注意wifi地址变动
       // public val address = "http://10.17.73.92/lazyDiary/App/"
    }

    private class Singleton{
        companion object {
            public val instant = NetworkManager()
        }
    }

    fun getUserService() = retrofit.create(UserService::class.java)
    fun getPlanService() = retrofit.create(PlanService::class.java)
    fun getDiaryService() = retrofit.create(DiaryService::class.java)
    fun getBillService() = retrofit.create(BillService::class.java)
}